package com.example.heimaai.controller;

import com.example.heimaai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.memory.ChatMemory.CONVERSATION_ID;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai")
public class CustomerSerivceController {

    private final ChatClient customerServiceClient;
    private final ChatHistoryRepository chatHistoryRepository;
//    @RequestMapping("/chatByBlock")
//    public String chat(String prompt){
//        return customerServiceClient.prompt()
//                .user(prompt)
//                .call()
//                .content();
//    }
    @RequestMapping(value = "/service" , produces = "text/html;charset=utf-8")
    public Flux<String> chatByFlux(String prompt,String chatId){
        chatHistoryRepository.save("service",chatId);
        return customerServiceClient.prompt()
                .user(prompt)
                .advisors(a->a.param(CONVERSATION_ID,chatId))
                .stream()
                .content();
    }

}
